package com.hlf.modules.api.controller;

import com.hlf.common.annotation.SysLog;
import com.hlf.common.constants.Constants;
import com.hlf.common.constants.FileConstants;
import com.hlf.common.utils.PageUtils;
import com.hlf.common.utils.ReturnResult;
import com.hlf.common.validator.Assert;
import com.hlf.modules.api.annotation.LoginUser;
import com.hlf.modules.api.interceptor.AuthorizationInterceptor;
import com.hlf.modules.merchant.store.entity.Store;
import com.hlf.modules.merchant.store.service.StoreService;
import com.hlf.modules.sys.controller.AbstractController;
import com.hlf.modules.sys.entity.File;
import com.hlf.modules.sys.entity.SysUserEntity;
import com.hlf.modules.sys.service.FileService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.map.HashedMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 店铺管理接口
 */
@RestController
@RequestMapping("/api/store")
@Api("门店管理接口")
public class ApiStoreController extends AbstractController{

    @Autowired
    private StoreService storeService;
    @Autowired
    private FileService fileService;

    /**
     * 查询门店列表(分页)
     */
    @PostMapping("list")
    @ApiOperation(value = "查询门店列表(分页)", notes = "查询门店列表(分页)")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "page", value = "当前页", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "limit", value = "每页显示条数", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult list(String page, String limit, HttpServletRequest request) {
        //获取userId
        Long userId = (Long) request.getAttribute(AuthorizationInterceptor.LOGIN_USER_KEY);
        Map<String, Object> map = new HashMap<>();
        map.put("userId", userId);
        map.put("page", page);
        map.put("limit", limit);
        PageUtils pageVO = storeService.queryListForPage(map);
        return ReturnResult.ok().put("data", pageVO);
    }
    /**
     * 查询单个门店

     @PostMapping("info")
     @ApiOperation(value = "查询单个店铺",notes = "查询单个店铺")
     @ApiImplicitParams({
     @ApiImplicitParam(paramType = "query", dataType="string", name = "storeId", value = "门店id", required = true),
     @ApiImplicitParam(paramType = "query", dataType="string", name = "token", value = "token", required = true)
     })
     public ReturnResult info(String storeId){
     Store store= storeService.queryObject(Long.parseLong(storeId));
     return ReturnResult.ok().put("data"tore);
     }
     */

    /**
     * 门店信息
     */
    @GetMapping("info")
    @ApiOperation(value = "门店信息", notes = "门店信息")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult info(@LoginUser SysUserEntity user) {
        Long storeId = Long.parseLong(user.getCurrentStoreId());
        Store store = storeService.queryObject(storeId);
        List<File> logoImgs = fileService.select(FileConstants.LOGO_IMG, store.getId());
        List<File> storeImgs = fileService.select(FileConstants.STORE_IMG, store.getId());
        Map<String, Object> map = new HashedMap();
        map.put("store", store);
        map.put("logoImg", logoImgs);
        map.put("storeImg", storeImgs);
        return ReturnResult.ok().put(Constants.DEFAULT_RESULT_NAME,map);
    }


    /**
     * 上传门店形象
     */
    @SysLog("上传门店形象")
    @PostMapping("uploadStore")
    @ApiOperation(value = "上传门店形象", notes = "上传门店形象")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "storeImgFilesJson", value = "门店形象照片json", required = false)
    })
    public ReturnResult uploadStore(@LoginUser SysUserEntity user, String storeImgFilesJson) {
        Assert.isBlank(storeImgFilesJson, "照片信息不能为空!");

        storeService.uploadStore(user, storeImgFilesJson);
        return ReturnResult.ok();
    }

    /**
     * 门店形象
     */
    @GetMapping("storeImg")
    @ApiOperation(value = "门店形象", notes = "门店形象")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true)
    })
    public ReturnResult uploadStore(@LoginUser SysUserEntity user) {
        return storeService.storeImg(user);
    }


//    /**
//     * 主体认证
//     */
//    @SysLog("主题认证")
//    @PostMapping("approved")
//    @ApiOperation(value = "主体认证", notes = "主体认证")
//    @ApiImplicitParams({
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "token", value = "token", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "merchantName", value = "企业名称", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "legalPerson", value = "法人", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "identityCardImgJson", value = "身份证照片", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "businessImg", value = "营业执照照片json", required = false),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "QSImg", value = "食品经营许可证照片json", required = false),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "license", value = "营业执照号", required = false),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "accountName", value = "开户人", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "province", value = "省", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "city", value = "市", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "district", value = "县", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "bankName", value = "开户银行", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "branchBankName", value = "开户支行", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "cardNo", value = "银行卡号", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "branchBankId", value = "银行支行id", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "type", value = "商户类型 1:对私账户 2:对公账户", required = true),
//            @ApiImplicitParam(paramType = "query", dataType = "string", name = "openAccountLicenseImg", value = "开户许可证", required = false)
//    })
//    public ReturnResult approved(@LoginUser SysUserEntity user, String merchantName, String legalPerson,
//                                 String identityCardImgJson, String businessImg, String QSImg, String license, String accountName,
//                                 String province, String city, String district, String bankName, String branchBankName, String cardNo,
//                                 String branchBankId, String type, String openAccountLicenseImg) {
//        Assert.isBlank(merchantName, "企业名称不能为空!");
//        Assert.isBlank(legalPerson, "法人不能为空!");
//        Assert.isBlank(identityCardImgJson, "身份证照片不能为空!");
//        Assert.isBlank(accountName, "开户人不能为空!");
//        Assert.isBlank(province, "省不能为空!");
//        Assert.isBlank(city, "市不能为空!");
//        Assert.isBlank(district, "县不能为空!");
//        Assert.isBlank(bankName, "开户银行不能为空!");
//        Assert.isBlank(branchBankName, "开户支行不能为空!");
//        Assert.isBlank(cardNo, "银行卡号不能为空!");
//        Assert.isBlank(branchBankId, "支行id不能为空!");
//        Assert.isBlank(type, "商户类型不能为空!");
//        if (Constants.BUSINESS_TYPE_OPEN.equals(Byte.valueOf(type))) {
//            Assert.isBlank(openAccountLicenseImg, "开户许可证照片不能为空!");
//        } else {
//            if (openAccountLicenseImg != null || openAccountLicenseImg.length() > 0) {
//                return ReturnResult.error("对私账户无需开户许可证!");
//            }
//        }
//
//        storeService.saveStoreDetail(user, merchantName, legalPerson, identityCardImgJson, businessImg, QSImg,
//                license, accountName, province, city, district, bankName, branchBankName, cardNo, branchBankId, type, openAccountLicenseImg);
//        return ReturnResult.ok();
//    }

}

